package com.jwgf.smarowindingmachine_unity.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

import com.jwgf.fault.FaultContent;
import com.jwgf.fault.FaultRecorder;
import com.jwgf.smarowindingmachine_unity.R;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.DialogFragment;

public class FaultDetailDialog extends DialogFragment {
	public static final String FAULT_CODE = "fault_code";
	private Spinner nameSpinner;
	private ImageView image;
	private TextView detail;

	private List<Integer> codeList = new ArrayList<Integer>();

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {

		LayoutInflater inflater = getActivity().getLayoutInflater();
		View v = inflater.inflate(R.layout.dialog_fault_detail, null);
		nameSpinner = (Spinner) v.findViewById(R.id.name);
		image = (ImageView) v.findViewById(R.id.image);
		detail = (TextView) v.findViewById(R.id.detail);
		detail.setTextSize(24);
		nameSpinner.setAdapter(new ArrayAdapter<String>(getActivity(),
				android.R.layout.simple_dropdown_item_1line, getData()));
		nameSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				TextView tv = (TextView)view;
				tv.setTextSize(26);
				tv.setGravity(android.view.Gravity.CENTER);
				updateUI(position);

			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {

			}
		});

		Bundle bd = getArguments();
		int code = bd.getInt(FAULT_CODE);
		if (code == 0) {
			updateUI(nameSpinner.getSelectedItemPosition());
		} else {
			int index = codeList.indexOf(Integer.valueOf(code));
			if (index != -1) {
				updateUI(index);
			} else {
				Toast.makeText(getActivity(), "故障码对应的导航不存在！", Toast.LENGTH_LONG);
			}
		}

		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setTitle("故障导航");
		builder.setView(v);
		return builder.create();
	}

	private List<String> getData() {
		List<String> list = new ArrayList<String>();
		HashMap<Integer, FaultContent> faultMap = FaultRecorder.getInstance()
				.getFaultMap();
		for (Entry<Integer, FaultContent> item : faultMap.entrySet()) {
			codeList.add(item.getKey());
			list.add(item.getValue().getName());
		}
		return list;
	}

	private void updateUI(int selection) {
		int selectCode = codeList.get(selection);
		int drawable_id = FaultRecorder.getInstance().getFaultMap()
				.get(selectCode).getDrawable_id();
		String detailText = FaultRecorder.getInstance().getFaultMap()
				.get(selectCode).getDetail();
		nameSpinner.setSelection(selection);
		if (drawable_id != 0) {
			image.setVisibility(View.VISIBLE);
			image.setImageResource(drawable_id);
		} else {
			image.setVisibility(View.GONE);
		}
		if (detailText != null) {
			detail.setText(detailText);
		} else {
			detail.setText("");
		}
	}

}
